<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="归属人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入归属人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观影状态" prop="watchStatus">
        <el-select v-model="queryParams.watchStatus" placeholder="请选择观影状态" clearable>
          <el-option
            v-for="dict in dict.type.flm_watch_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.flm_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['film:filmList:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['film:filmList:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['film:filmList:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['film:filmList:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="filmListList" @selection-change="handleSelectionChange" :row-style="{height:'20px'}" :cell-style="{padding:'5px'}">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="归属人" align="center" prop="userId" />
      <el-table-column label="电影" align="center" prop="filmInfo.name" />
      <el-table-column label="我的评分" align="center" prop="score" />
      <el-table-column label="观影状态" align="center" prop="watchStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flm_watch_status" :value="scope.row.watchStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="观影日期" align="center" prop="watchDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.watchDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="观影方式" align="center" prop="watchType" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flm_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateNo" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['film:filmList:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['film:filmList:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改影单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-col :span="12">
            <el-form-item label="归属人" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入归属人" :disabled = "editFlag_page"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电影" prop="filmInfo">
              <el-select v-model="form.filmInfo" value-key="id" placeholder="请选择电影"
                         :disabled = "editFlag_page"
                         clearable
                         filterable
                         @change="handleFilmInfo">
                <el-option
                  v-for="dict in films"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-show="showFilmInfo">
            <el-form-item label="上映日期" prop="showTime">
              <el-date-picker clearable
                              v-model="form.filmInfo.showTime"
                              type="date"
                              disabled
                              value-format="yyyy-MM-dd"
                              placeholder="请选择上映日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="showFilmInfo">
            <el-form-item label="产地" prop="area">
              <el-input v-model="form.filmInfo.area" placeholder="请输入产地" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="showFilmInfo">
            <el-form-item label="时长" prop="duration">
              <el-input v-model="form.filmInfo.duration" placeholder="请输入时长" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show="showFilmInfo">
            <el-form-item label="客观评分" prop="score">
              <el-input v-model="form.filmInfo.score" placeholder="客观评分" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show="showFilmInfo">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.filmInfo.title" type="textarea" placeholder="请输入内容" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="观影状态" prop="watchStatus">
              <el-select v-model="form.watchStatus" placeholder="请选择观影状态" @change="handleWatchInfo">
                <el-option
                  v-for="dict in dict.type.flm_watch_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-show = "showWatchInfo">
            <el-form-item label="观影日期" prop="watchDate">
              <el-date-picker clearable
                              v-model="form.watchDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择观影日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show = "showWatchInfo">
            <el-form-item label="观影方式" prop="watchCount">
              <el-input v-model="form.watchType" placeholder="请输入观影方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show = "showWatchInfo">
            <el-form-item label="观影次数" prop="watchCount">
              <el-input v-model="form.watchCount" placeholder="请输入观影次数" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-show = "showWatchInfo">
            <el-form-item label="我的评分" prop="score">
              <el-input v-model="form.score" placeholder="请输入我的评分" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.flm_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show = "showWatchInfo">
            <el-form-item label="我的影评" prop="comment">
              <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show = "showWatchInfo">
            <el-form-item label="观影备注" prop="watchRemark">
              <el-input v-model="form.watchRemark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show = "showWatchInfo">
            <el-form-item label="观后感" prop="watchThink" >
              <el-input v-model="form.watchThink" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listFilmList, getFilmList, delFilmList, addFilmList, updateFilmList} from "@/api/film/filmList";

export default {
  name: "FilmList",
  dicts: ['flm_watch_status', 'flm_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      //打开页面后是否允许编辑
      editFlag_page: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showFilmInfo:false,
      showWatchInfo: false,
      // 总条数
      total: 0,
      // 影单管理表格数据
      filmListList: [],
      //电影信息
      films : [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        filmId: null,
        watchStatus: null,
        status: null,
      },

      // 表单参数
      form: {
        filmInfo: {
          showTime:null
        }
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "归属人不能为空", trigger: "blur" }
        ],
        filmId: [
          { required: true, message: "电影ID不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        flag: [
          { required: true, message: "标志不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询影单管理列表 */
    getList() {
      this.loading = true;
      listFilmList(this.queryParams).then(response => {
        this.filmListList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        filmId: null,
        score: null,
        comment: null,
        watchStatus: "00",
        watchDate: null,
        watchCount: null,
        watchType: null,
        watchRemark: null,
        watchThink: null,
        status: "00",
        flag: null,
        createNo: null,
        createTime: null,
        updateNo: null,
        updateTime: null,
        // filmName: null,
        // filmShowTime: null,
        // filmArea: null,
        // filmDuration: null,
        // filmScore: null,
        // filmTitle: null,
        filmInfo: {
          showTime:null
        },
      };
      this.showFilmInfo = false;
      this.showWatchInfo = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getFilmList().then(response => {
        this.films = response.films;
        this.open = true;
        this.title = "添加影单管理";
        this.editFlag_page = false;//可用
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFilmList(id).then(response => {
        this.films = response.films;
        this.form = response.data;
        this.showFilmInfo = true;
        this.editFlag_page = true;//不可用
        this.open = true;
        this.handleShowWatchInfo(this.form.watchStatus);
        this.title = "修改影单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFilmList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFilmList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除影单管理编号为"' + ids + '"的数据项？').then(function() {
        return delFilmList(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('film/filmList/export', {
        ...this.queryParams
      }, `filmList_${new Date().getTime()}.xlsx`)
    },
    handleFilmInfo(row){
      this.form.filmId = row.id;
      // this.form.filmInfo.id = row.id;
      // // this.form.filmName = row.name;
      // this.form.filmInfo.name = row.name;
      // // this.form.filmShowTime= row.showTime;
      // this.form.filmInfo.showTime= row.showTime;
      // // this.form.filmArea= row.area;
      // this.form.filmInfo.area= row.area;
      // // this.form.filmDuration= row.duration;
      // this.form.filmInfo.duration= row.duration;
      // // this.form.filmScore= row.score;
      // this.form.filmInfo.score= row.score;
      // // this.form.filmTitle= row.title;
      // this.form.filmInfo.title= row.title;
      this.form.filmInfo = {
        id:row.id,
        name:row.name,
        showTime:row.showTime,
        area:row.area,
        duration:row.duration,
        score:row.score,
        title:row.title,
      }
      this.showFilmInfo = true;
      console.log("row:%o",row);
    },
    handleWatchInfo(row){
      console.log("dci:%o",row)
      this.handleShowWatchInfo(row);
      if (row == "00") {
        this.form.watchCount = 0;
      } else if (row == "01" || row == "02"){
          this.form.watchCount = 1;
      } else {
        this.form.watchCount = null;
      }
    },
    handleShowWatchInfo(watchStatus){
      if (watchStatus == "00") {
        this.showWatchInfo = false;
      } else {
        this.showWatchInfo = true;
      }
    }
  }
};
</script>
